package editro.view.components {
	import editro.model.AntTargetVO;
	import editro.model.ATECacheModel;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import org.osflash.signals.Signal;

	/**
	 * ...
	 * @author LEEF LEE_LEAF918@163.COM_QQ969442267
	 */
	public class AntTaskView extends Sprite {
		public var dragingSignal:Signal;
		public var archorSelectedSignal:Signal;
		private var bgShape:Shape;
		private var isDraging:Boolean;
		private var archorList:Array;
		private var _vo:AntTargetVO;
		private var titleTf:TextField;
		//[Inject] public var model:ATECacheModel;
		
		public function AntTaskView(){
			this.doubleClickEnabled = true;
		}

		/**
		 *进行初始化设置，根据参数初始本个AntTaskView的基本信息
		 * @param	width
		 * @param	height
		 * @param	bgColor
		 */
		public function init(vo:AntTargetVO, width:Number, height:Number, bgColor:int, archors:uint):void {
			this._vo = vo;
			this._vo.viewHolder = this;
			//
			archorList = this.vo.archorsList;
			for (var i:int = 0; i < archors; i++) 
			{
				var item:ArchorForLinkage = new ArchorForLinkage;
				addChild(item);
				item.addEventListener(MouseEvent.CLICK, onClickArchorHandler);
				archorList.push(item);
			}
			drawBg(width, height, bgColor);
			var tf:TextFormat = new TextFormat();
			//tf.align=TextFormatAlign.CENTER
			tf.font = "Georgia";
			tf.color = 0x4F5D4E;
			tf.size = 16;
			tf.bold = true;
			tf.italic = true;
			
			titleTf = new TextField();
			titleTf.width = width;
			titleTf.autoSize = TextFieldAutoSize.CENTER;
			titleTf.selectable = false;
			titleTf.defaultTextFormat = tf;
			addChildAt(titleTf,1);
			titleTf.text = vo.targetName;
			//
			dragingSignal = new Signal();
			archorSelectedSignal = new Signal(uint);
			//
			addEventListener(MouseEvent.MOUSE_DOWN, goDrag);
			addEventListener(MouseEvent.MOUSE_UP, stpDrag);
			addEventListener(MouseEvent.MOUSE_MOVE, patchDraging);
		}
		
		/**
		 * 点击一个ARCHOR,选择遍历出此ARCHOR的序号，发出信号
		 * @param	e
		 */
		private function onClickArchorHandler(e:MouseEvent):void 
		{
			var currentItem:ArchorForLinkage = e.currentTarget as ArchorForLinkage;
			//TODO:这里发出的是左上角的注册点，应该是中心点
			for (var i:int = 0; i < archorList.length; i++) 
			{
				var item:ArchorForLinkage = archorList[i] as ArchorForLinkage;
				if (currentItem == item) {
					//当前的坐标
					//var p:Point = new Point(item.x, item.y);
					//archorSelectedSignal.dispatch(this.localToGlobal(p));
					archorSelectedSignal.dispatch(i);
					break;
				}
			}
		}
		
		private function patchDraging(e:MouseEvent):void 
		{
			if (isDraging) {
				dragingSignal.dispatch();
			}
		}
		
		public function getArchorInStagePoint(archorIndex:uint):Point 
		{
			var archr:ArchorForLinkage = archorList[archorIndex];
			return this.localToGlobal(new Point(archr.x, archr.y));
		}
		private function stpDrag(e:MouseEvent):void {
			isDraging = false;
			this.stopDrag();
		}

		private function goDrag(e:MouseEvent):void {
			isDraging = true;
			this.startDrag();
		}

		/**
		 * 画出背景
		 * @param	width
		 * @param	height
		 * @param	bgColor
		 */
		private function drawBg(width:Number, height:Number, bgColor:int):void {
			if (bgShape == null){
				bgShape = new Shape;
				addChildAt(bgShape, 0);
			}
			bgShape.graphics.beginFill(bgColor, 0.9);
			bgShape.graphics.drawRoundRect(0, 0, width, height, 5, 5);
			bgShape.graphics.endFill();
			//
			relayoutLinkArchors();
		}

		/**
		 * 重新放置四个锚点
		 */
		private function relayoutLinkArchors():void {
			//分为4组,每组的个数,放在四个角上。
			var perGroup:uint = Math.floor(archorList.length / 4);
			var item: ArchorForLinkage;
			var i:int = 0;
			var verticalGap:uint = 15;
			//左上角放置,1
			for (i= 0; i < perGroup; i++) 
			{
				item = archorList[i+perGroup*0] as ArchorForLinkage
				item.x = bgShape.x + 2;
				item.y=bgShape.y+verticalGap*i;
			}
			//右上角放置,2
			for (i= 0; i < perGroup; i++) 
			{
				item= archorList[i+perGroup*1] as ArchorForLinkage
				item.x = bgShape.width- 2;
				item.y=bgShape.y+verticalGap*i;
			}
			//左下角放置,3
			for (i= 0; i < perGroup; i++) 
			{
				item = archorList[i+perGroup*2] as ArchorForLinkage
				item.x = bgShape.x + 2;
				item.y=bgShape.height-verticalGap*i;
			}
			//右下角放置,4,可能大于pergroup 的数值
			for (i= 0; i < archorList.length-perGroup*3; i++) 
			{
				item= archorList[i+perGroup*3] as ArchorForLinkage
				item.x = bgShape.width- 2;
				item.y=bgShape.height-verticalGap*i;
			}
		}
		
		public function get vo():AntTargetVO 
		{
			return _vo;
			
		}
	}

}